<?php
/*
 * @Author: 莫卓才
 * @eMail: handsome.mo@foxmail.com
 * @Descripttion: 描述
 * @version: 1.0.0
 * @Date: 2020-03-24 16:03:15
 * @LastEditors: 莫卓才
 * @LastEditTime: 2020-03-26 10:35:25
 */
declare (strict_types = 1);

namespace app\index\controller;

use app\common\model\NewsModel;
use app\common\model\NewsCategoryModel;
use think\facade\View;

class News extends Common
{
	protected function initialize()
	{
		parent::initialize(); // TODO: Change the autogenerated stub


	}

	public function index()
    {
        $cid = input('cid',1);
        $map = [];
        if(!empty($cid)){
	        $categoryInfo = NewsCategoryModel::where('id',$cid)->find();
	        if($categoryInfo->pid===0){
	        	$categorySubIdArr = NewsCategoryModel::where('pid',$categoryInfo->id)->column('id');
	        	array_push($categorySubIdArr,$cid);
		        $map[] = ['category_id','in',$categorySubIdArr];
	        }else{
		        $map[] = ['category_id','=',$cid];
	        }

        }


	    $map[] = ['status','=',1];
        $list = NewsModel::pageList($map,12);
        // 获取分页显示
        $page = $list->render();


	    $currentCategory = NewsCategoryModel::where('id',$cid)->find();
	    View::assign('cid',$cid);


	    if(!empty($currentCategory->pid)){
		    $categoryPid = $currentCategory->pid;
	    }else{
		    $categoryPid = $currentCategory->id;

	    }
	    $currentCategoryList = NewsCategoryModel::where('pid',$categoryPid)->whereOr('id',$categoryPid)->order('sort','ASC')->select();
	    View::assign('currentCategoryList',$currentCategoryList);



        return view('index',['list'=>$list,'page'=>$page]);
    }

    public function show(){
        $id = input('id');
        $act = input('act','');

        $info = NewsModel::info($id);
		if(empty($info)){
			echo '暂无数据';
			exit;
		}

        NewsModel::where('id', $id)->inc('clicks', 1)->update();

        $prev = NewsModel::prev($info,1);
        $next = NewsModel::next($info,1);

        View::assign('info',$info);
        View::assign('prev',$prev);
        View::assign('next',$next);

	    if(!empty($info)){
		    $currentCategory = NewsCategoryModel::find($info->category_id);
		    if($this->lang==='en'){
		    	$currentName = $currentCategory->ename;
		    }else{
			    $currentName = $currentCategory->name;
		    }
		    View::assign('currentCategoryName',$currentName);
		    View::assign('cid',$info->category_id);

		    if(!empty($currentCategory->pid)){
		    	$categoryPid = $currentCategory->pid;
		    }else{
			    $categoryPid = $currentCategory->id;

		    }
		    $currentCategoryList = NewsCategoryModel::where('pid',$categoryPid)->whereOr('id',$categoryPid)->order('sort','ASC')->select();
		    View::assign('currentCategoryList',$currentCategoryList);
	    }

        return view('show');
    }

	public function search(){
		$keyword = input('keyword');
		$map = [];
		if(!empty($keyword)){
			$map[] = ['title','like','%'.$keyword.'%'];
		}
		$map[] = ['lang','=',$this->lang];
		$map[] = ['status','=',1];
		$list = NewsModel::pageList($map,10);
		// 获取分页显示
		$page = $list->render();

		return view('',['list'=>$list,'page'=>$page]);
	}


}
